using DocBot.Objects;
using DocBot.Util;
using DocBot.Util.Collections;

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

using System;
using System.IO;
using System.Windows.Input;
using System.Xml.Serialization;

namespace DocBot.ViewModel
{
	public class PlaylistViewModel : ViewModelBase
	{
		#region Members
		private static string configFileName = "Playlist.xml";
		private LocalDataBase _database = new LocalDataBase();
		ThreadSafeObservableCollection<PlaylistItem> _songs = new ThreadSafeObservableCollection<PlaylistItem>();
		int _selectedPlayListItem = -1;
		#endregion

		#region Properties
		public Playmode PlayType { get; set; }

		public ThreadSafeObservableCollection<PlaylistItem> Songs
		{
			get
			{
				return _songs;
			}
			set
			{
				_songs = value;
			}
		}

		public int SelectedPlayListItem
		{
			get
			{
				return this._selectedPlayListItem;
			}
			set
			{
				if (value != this._selectedPlayListItem)
				{
					this._selectedPlayListItem = value;
				}
			}
		}
		#endregion

		#region Construction
		public PlaylistViewModel()
		{
			if (IsInDesignMode)
			{
				LoadSongs(configFileName);
				if (_songs.Count == 0)
					for (int i = 0; i < 3; ++i)
					{
						_songs.Add(new PlaylistItem { ArtistName = _database.GetRandomArtistName, SongTitle = _database.GetRandomSongTitle });
					}
			}
			else
			{
				LoadSongs(configFileName);
			}
		}

		~PlaylistViewModel()
		{
			if (!IsInDesignMode)
				SaveSongs(configFileName);
		}
		#endregion

		#region Methods
		public bool checkSongs(String url)
		{
			foreach (PlaylistItem p in _songs)
			{
				if (p.Url == url)
					return true;
			}
			return false;
		}

		public void LoadSongs(String configFileName)
		{

			if (File.Exists(Settings.Instance.configFilePath + "\\" + configFileName))
			{
				XmlSerializer deserializer = new XmlSerializer(typeof(ThreadSafeObservableCollection<PlaylistItem>));
				TextReader textReader = new StreamReader(Settings.Instance.configFilePath + "\\" + configFileName);
				ThreadSafeObservableCollection<PlaylistItem> _object = (ThreadSafeObservableCollection<PlaylistItem>)deserializer.Deserialize(textReader);
				textReader.Close();
				if (_object == null)
				{
					_object = new ThreadSafeObservableCollection<PlaylistItem>();
				}
				_songs = _object;
			}
		}

		private void SaveSongs(String configFileName)
		{
			if (_songs.Count > 0)
			{
				XmlSerializer s = new XmlSerializer(typeof(ThreadSafeObservableCollection<PlaylistItem>));
				TextWriter writer = new StreamWriter(Settings.Instance.configFilePath + "\\" + configFileName);
				s.Serialize(writer, _songs);
				writer.Close();
			}
			else
			{
				File.Delete(Settings.Instance.configFilePath + "\\" + configFileName);
			}
		}
		#endregion

		#region Clear Playlist
		void ClearExecute()
		{
			if (_songs == null)
				return;

			this._selectedPlayListItem = -1;
			_songs.Clear();
		}

		bool CanClearExecute()
		{
			return true;
		}

		public ICommand ClearItems { get { return new RelayCommand(ClearExecute, CanClearExecute); } }
		#endregion

		#region RemoveItem
		void RemoveExecute()
		{
			if (_songs == null)
				return;
			if (_selectedPlayListItem > -1)
				if (_selectedPlayListItem <= (_songs.Count - 1))
					_songs.Remove(_songs[_selectedPlayListItem]);
		}

		bool CanRemoveExecute()
		{
			return true;
		}
		public void RemoveItemMethod()
		{
			RemoveExecute();
		}

		public ICommand RemoveItem { get { return new RelayCommand(RemoveExecute, CanRemoveExecute); } }
		#endregion
	}
}